home *** CD-ROM | disk | FTP | other *** search
- /* AEObj.c
- * Utilities for dealing with Apple Event Objects (making descriptors, etc.)
- * ©1992 Working Software, Inc.
- * This source code is copyrighted. Permission is granted to use the Word Services
- * portion of the Writeswell Jr. source code in your own programs, but you
- * may not distribute the Writeswell Jr. word-processor code as a
- * commercial product. If you modify the code, please do not call it
- * Writeswell Jr. (or Writeswell.) This will ensure that people understand the
- * program and don’t have to deal with a number of different versions with
- * who-knows-what going on in the code.
- *
- * Writeswell Jr. and Writeswell are trademarks of Working Software, Inc.
- * 18 Dec 91 Mike Crawford
- */
-
- #include <AppleEvents.h>
- #include <AEObjects.h>
- #include <AERegistry.h>
- #include "TBConstants.h"
- #include "TBGlobals.h"
- #include "AEObj.h"
- #include "ObWind.h"
- #include "ObText.h"
- #include "ObOspec.h"
- #include "Gripe.h"
-
- OSErr InstallMyAccessors( void );
- pascal OSErr MyCountProc(DescType desiredType,
- DescType containerClass,
- const AEDesc *container,
- long *result);
- pascal OSErr MyDisposeTokenProc(AEDesc *unneededToken);
- OSErr InstallMyCoercers( void );
- extern pascal OSErr AEObjectInit(void);
-
- // OSL Callbacks
- static OSLCountUPP gCountUPP = (OSLCountUPP)NULL;
- static OSLDisposeTokenUPP gDisposeTokenUPP = (OSLDisposeTokenUPP)NULL;
-
- // Object Accessors
- static OSLAccessorUPP gWindFromNullUPP = (OSLAccessorUPP)NULL;
- static OSLAccessorUPP gPropFromWindUPP = (OSLAccessorUPP)NULL;
- static OSLAccessorUPP gTextFromWindUPP = (OSLAccessorUPP)NULL;
- static OSLAccessorUPP gWordFromTETextUPP = (OSLAccessorUPP)NULL;
- static OSLAccessorUPP gCharFromTETextUPP = (OSLAccessorUPP)NULL;
- static OSLAccessorUPP gPropFromTETextUPP = (OSLAccessorUPP)NULL;
- static OSLAccessorUPP gOspecFromWindUPP = (OSLAccessorUPP)NULL; // Special for Word Services
-
- // Coercion Handlers
- static AECoercePtrUPP gTextPtrToPStringUPP = (AECoercePtrUPP)NULL;
-
- OSErr InitAEObjStuff( void )
- {
- OSErr err;
-
- err = AEObjectInit();
- if ( err ){
- Gripe( "\pAEObjectInit failed" );
- return err;
- }
-
- err = InstallMyAccessors();
- if ( err ){
- Gripe( "\pInstallMyAccessors failed" );
- return err;
- }
-
- gCountUPP = NewOSLCountProc( MyCountProc );
- if ( !gCountUPP )
- return memFullErr;
-
- gDisposeTokenUPP = NewOSLDisposeTokenProc( MyDisposeTokenProc );
- if ( !gDisposeTokenUPP )
- return memFullErr;
-
- /* Install the callback functions used by the AEObj parser */
- err = AESetObjectCallbacks((OSLCompareUPP)nil,
- (OSLCountUPP)gCountUPP,
- (OSLDisposeTokenUPP)gDisposeTokenUPP,
- (OSLGetMarkTokenUPP)nil,
- (OSLMarkUPP)nil,
- (OSLAdjustMarksUPP)nil,
- (OSLGetErrDescUPP)nil);
-
- err = InstallMyCoercers();
- if ( err ){
- Gripe( "\pInstallMyCoercers failed" );
- return err;
- }
-
- return noErr;
- }
-
- OSErr TearDownAEObjStuff( void )
- {
- #ifdef GENERATINGCFM
-
- // We don't want to dispose of the UPP if we are not running
- // under the code fragment manager, as the UPP is an actual
- // pointer to a function in that case.
-
- // OSL Callback functions
-
- if ( gCountUPP )
- DisposeRoutineDescriptor( gCountUPP );
-
- if ( gDisposeTokenUPP )
- DisposeRoutineDescriptor( gDisposeTokenUPP );
-
- // Object Accessors
-
- if ( gWindFromNullUPP )
- DisposeRoutineDescriptor( gWindFromNullUPP );
-
- if ( gPropFromWindUPP )
- DisposeRoutineDescriptor( gPropFromWindUPP );
-
- if ( gTextFromWindUPP )
- DisposeRoutineDescriptor( gTextFromWindUPP );
-
- if ( gWordFromTETextUPP )
- DisposeRoutineDescriptor( gWordFromTETextUPP );
-
- if ( gCharFromTETextUPP )
- DisposeRoutineDescriptor( gCharFromTETextUPP );
-
- if ( gPropFromTETextUPP )
- DisposeRoutineDescriptor( gPropFromTETextUPP );
-
- if ( gOspecFromWindUPP )
- DisposeRoutineDescriptor( gOspecFromWindUPP );
-
- // Coercion Handlers
-
- if ( gTextPtrToPStringUPP )
- DisposeRoutineDescriptor( gTextPtrToPStringUPP );
-
- #endif
- return noErr;
- }
-
- OSErr InstallMyAccessors( void )
- {
- OSErr err;
-
- /* We install the accessors to get various objects from various containers */
-
- /* Get a Window element from an application (null) container */
-
- gWindFromNullUPP = NewOSLAccessorProc( WindFromNull );
- if ( !gWindFromNullUPP )
- return memFullErr;
-
- err = AEInstallObjectAccessor(cWindow,
- typeNull,
- (OSLAccessorUPP)gWindFromNullUPP,
- 0,
- false);
-
- /* Get any property from a window container */
-
- gPropFromWindUPP = NewOSLAccessorProc( PropFromWind );
- if ( !gPropFromWindUPP )
- return memFullErr;
-
- err = AEInstallObjectAccessor( typeProperty,
- cWindow,
- (OSLAccessorUPP)gPropFromWindUPP,
- 0,
- false);
-
- /* Get text from a window container */
- gTextFromWindUPP = NewOSLAccessorProc( TextFromWind );
- if ( !gTextFromWindUPP )
- return memFullErr;
-
- err = AEInstallObjectAccessor( cText,
- cWindow,
- (OSLAccessorUPP)gTextFromWindUPP,
- 0,
- false);
-
- /* Get a word from a TextEdit text container */
- gWordFromTETextUPP = NewOSLAccessorProc( WordFromTEText );
- if ( !gWordFromTETextUPP )
- return memFullErr;
-
- err = AEInstallObjectAccessor( cWord,
- typeTEText,
- (OSLAccessorUPP)gWordFromTETextUPP,
- 0,
- false);
-
- /* Get a char (or set thereof) from a TextEdit text container */
- gCharFromTETextUPP = NewOSLAccessorProc( CharFromTEText );
- if ( !gCharFromTETextUPP )
- return memFullErr;
-
- err = AEInstallObjectAccessor( cChar,
- typeTEText,
- (OSLAccessorUPP)gCharFromTETextUPP,
- 0,
- false);
-
- /* Get any property from a text container */
- gPropFromTETextUPP = NewOSLAccessorProc( PropFromTEText );
- if ( !gPropFromTETextUPP )
- return memFullErr;
-
- err = AEInstallObjectAccessor( typeProperty,
- typeTEText,
- (OSLAccessorUPP)gPropFromTETextUPP,
- 0,
- false);
-
- /* Get an object specifier from a window container (for Word Services) */
- gOspecFromWindUPP = NewOSLAccessorProc( OspecFromWind );
- if ( !gOspecFromWindUPP )
- return memFullErr;
-
- err = AEInstallObjectAccessor( typeObjectSpecifier,
- cWindow,
- (OSLAccessorUPP)gOspecFromWindUPP,
- 0,
- false);
-
- return noErr;
- }
-
- OSErr InstallMyCoercers( void )
- {
- OSErr err;
-
- // Note that we're using a pointer, rather than descriptor based coercion
- // function here. No particular reason why.
-
- gTextPtrToPStringUPP = NewAECoercePtrProc( TextPtrToPString );
- if ( !gTextPtrToPStringUPP )
- return memFullErr;
-
- // I think there is a bug in the Universal Headers, with AECoercePtrUPP vs AECoercionHandlerUPP
-
- err = AEInstallCoercionHandler( typeChar,
- typePString,
- (void*)(AECoercePtrUPP)gTextPtrToPStringUPP,
- 0L,
- false, /* Pass a pointer not a descriptor */
- false ); /* Application table, not System */
- if ( err ){
- Gripe( "\pAEInstallCoercionHandler failed on TextPtrToPString" );
- return err;
- }
-
- return noErr;
- }
-
- /* This is a single routine that must know how to count each of the kinds of objects
- * that are used in this application. Eventually we should have it call functions
- * that are in the ObjXXX files.
- */
-
- pascal OSErr MyCountProc(DescType desiredType,
- DescType containerClass,
- const AEDesc *container,
- long *result)
- {
- OSErr err = noErr;
-
- switch ( desiredType ){
- case cWindow:
- *result = ( gDocWindow ? 1 : 0 );
- break;
- default:
- Gripe( "\pTrying to count an object I do not know about" );
- err = errAENoSuchObject;
- break;
- }
-
- return err;
- }
-
- pascal OSErr MyDisposeTokenProc(AEDesc *unneededToken)
- {
- OSErr myErr = noErr;
-
- /* In most cases we just toss out the token descriptor. We may need to do more
- * stuff in the event the the token handle actually points to some real data.
- */
-
- switch (unneededToken->descriptorType) {
- case cWindow:
- myErr = AEDisposeDesc(unneededToken);
- break;
-
- default:
- /* I default to just disposing of the token, ne ces pa */
- AEDisposeDesc(unneededToken);
- break;
- }
- return(myErr);
- }
-
- OSErr MakeTextDesc( AEDesc *descPtr )
- {
-
- return noErr;
- }